#ifndef __STEPMOTOR_H
#define __STEPMOTOR_H

#include <stdint.h>
//(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
#define TIM_OCnInit(TIMx, TIM_OCInitStruct, Channel) TIM_OC##Channel##Init(TIMx, TIM_OCInitStruct)
//------- stepmotor -------------------------------------
// 74hc14 - inverter on the step motors board. STEP on falling edge
//  _______        ______
//         x      |
//         |______|

#define MX_STEP_ON          GPIO_SetBits
#define MX_STEP_OFF         GPIO_ResetBits
//M0 defines(A)
#define M0_TIM              TIM2
#define M0_TIM_IRQn         TIM2_IRQn
#define M0_TIM_IRQHandler   TIM2_IRQHandler
#define M0_TIM_CLK          RCC_APB1Periph_TIM2
#define M0_TIM_CHN          2

#define M0_EN_PORT          GPIOF
#define M0_EN_PIN           GPIO_Pin_3
#define M0_DIR_PORT         GPIOF
#define M0_DIR_PIN          GPIO_Pin_2
#define M0_STEP_PORT        GPIOA
#define M0_STEP_PIN         GPIO_Pin_1

//M1 defines(Y)
#define M1_TIM              TIM3
#define M1_TIM_IRQn         TIM3_IRQn
#define M1_TIM_IRQHandler   TIM3_IRQHandler
#define M1_TIM_CLK          RCC_APB1Periph_TIM3
#define M1_TIM_CHN          2

#define M1_EN_PORT          GPIOG
#define M1_EN_PIN           GPIO_Pin_7
#define M1_DIR_PORT         GPIOG
#define M1_DIR_PIN          GPIO_Pin_6
#define M1_STEP_PORT        GPIOA
#define M1_STEP_PIN         GPIO_Pin_9

//M2 defines(Z)
#define M2_TIM              TIM4
#define M2_TIM_IRQn         TIM4_IRQn
#define M2_TIM_IRQHandler   TIM4_IRQHandler
#define M2_TIM_CLK          RCC_APB1Periph_TIM4
#define M2_TIM_CHN          1

#define M2_EN_PORT          GPIOF
#define M2_EN_PIN           GPIO_Pin_1
#define M2_DIR_PORT         GPIOF
#define M2_DIR_PIN          GPIO_Pin_0
#define M2_STEP_PORT        GPIOA
#define M2_STEP_PIN         GPIO_Pin_0

//M3 defines(X)

#define M3_TIM              TIM5
#define M3_TIM_IRQn         TIM5_IRQn
#define M3_TIM_IRQHandler   TIM5_IRQHandler
#define M3_TIM_CLK          RCC_APB1Periph_TIM5
#define M3_TIM_CHN          1

#define M3_EN_PORT          GPIOG
#define M3_EN_PIN           GPIO_Pin_4
#define M3_DIR_PORT         GPIOG
#define M3_DIR_PIN          GPIO_Pin_5
#define M3_STEP_PORT        GPIOA
#define M3_STEP_PIN         GPIO_Pin_8


#define STEPS_MOTORS        4


/*
 *	One ENABLE for all motors
 */

void stepm_init(void);
void stepm_proc(uint8_t id);

void stepm_EmergeStop(void);
extern uint8_t stepmPause;

void stepm_addMove(uint32_t steps[], uint32_t frq[], uint8_t dir[]);

uint32_t stepm_LinesBufferIsFull(void);
int32_t stepm_getRemainLines(void);

int32_t stepm_getCurGlobalStepsNum(uint8_t id);
void stepm_ZeroGlobalCrd(void);
int32_t stepm_inProc(void);
uint32_t stepm_getRemSteps(uint8_t id);
void step_dump(void);

#endif /* STEPMOTOR_H_ */
